package com.xiaoshuidi.cloud.module.contract.controller.admin.fee;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.contract.controller.admin.fee.vo.*;
import com.xiaoshuidi.cloud.module.contract.convert.fee.FeesConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.fee.FeesDO;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.service.fee.FeesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 费项配置")
@RestController
@RequestMapping(ApiConstants.NAME+"/config/fees")
@Validated
public class FeesController {

    @Resource
    private FeesService feesService;

    @PostMapping("/create")
    @Operation(summary = "创建费项配置")
    //@PreAuthorize("@ss.hasPermission('config:fees:create')")
    public CommonResult<Long> createFees(@Valid @RequestBody FeesCreateReqVO createReqVO) {
        return success(feesService.createFees(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新费项配置")
    //@PreAuthorize("@ss.hasPermission('config:fees:update')")
    public CommonResult<Boolean> updateFees(@Valid @RequestBody FeesUpdateReqVO updateReqVO) {
        feesService.updateFees(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除费项配置")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('config:fees:delete')")
    public CommonResult<Boolean> deleteFees(@RequestParam("id") Integer id) {
        feesService.deleteFees(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得费项配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('config:fees:query')")
    public CommonResult<FeesRespVO> getFees(@RequestParam("id") Integer id) {
        FeesDO fees = feesService.getFees(id);
        return success(FeesConvert.INSTANCE.convert(fees));
    }

    @GetMapping("/list")
    @Operation(summary = "获得费项配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    //@PreAuthorize("@ss.hasPermission('config:fees:query')")
    public CommonResult<List<FeesRespVO>> getFeesList(@RequestParam("ids") Collection<Integer> ids) {
        List<FeesDO> list = feesService.getFeesList(ids);
        return success(FeesConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/save")
    @Operation(summary = "保存费项配置列表")
    //@PreAuthorize("@ss.hasPermission('config:fees:save')")
    public CommonResult<Boolean> saveFeesList(@Valid @RequestBody Collection<FeeSaveVo> feeSaveVos) {
        feesService.updateFeesList(feeSaveVos);
        return success(true);
    }

    @GetMapping("/list-all")
    @Operation(summary = "获得费项配置列表")
    ////@PreAuthorize("@ss.hasPermission('config:fees:query')")
    public CommonResult<List<FeesRespVO>> getFeesListAll() {
        return success(feesService.getFeesListAll());
    }

    @GetMapping("/get-fees")
    @Operation(summary = "通过费项类型获取使用的费项列表")
    public CommonResult<List<FeesListRespVO>> getFeesListByFeeTypeId(@RequestParam("feeSort") String feeSort) {
        List<FeesListRespVO> list = feesService.getFeesListByFeeType(feeSort);
        return success(list);
    }

    @GetMapping("/type/list")
    @Operation(summary = "获得费项列表")
    ////@PreAuthorize("@ss.hasPermission('config:fees:query')")
    public CommonResult<List<FeesRespVO>> getFeesTypeList() {
        return success(feesService.getFeesTypeListAll());
    }
//    @GetMapping("/page")
//    @Operation(summary = "获得费项配置分页")
//    //@PreAuthorize("@ss.hasPermission('config:fees:query')")
//    public CommonResult<PageResult<FeesRespVO>> getFeesPage(@Valid FeesPageReqVO pageVO) {
//        PageResult<FeesDO> pageResult = feesService.getFeesPage(pageVO);
//        return success(FeesConvert.INSTANCE.convertPage(pageResult));
//    }
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出费项配置 Excel")
//    //@PreAuthorize("@ss.hasPermission('config:fees:export')")
//    @OperateLog(type = EXPORT)
//    public void exportFeesExcel(@Valid FeesExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<FeesDO> list = feesService.getFeesList(exportReqVO);
//        // 导出 Excel
//        List<FeesExcelVO> datas = FeesConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "费项配置.xls", "数据", FeesExcelVO.class, datas);
//    }

}
